package com.simple.util.poi;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @program: simple_tools
 * @description: 浏览器下载Excel工具类
 * @author: ChenWenLong
 * @create: 2019-12-30 15:23
 **/
public class DownloadUtils {

    /**
     * 功能描述:
     * 〈浏览器下载Excel工具类〉
     *
     * @params : [byteArrayOutputStream, response, returnName]
     * @return : void
     * @author : cwl
     * @date : 2019/12/30 15:23
     */
    public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException {
        response.setContentType("application/octet-stream");
        returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1"));			//保存的文件名,必须和页面编码一致,否则乱码
        response.addHeader("Content-Disposition","attachment;filename=total.xls");
        response.setContentLength(byteArrayOutputStream.size());
        response.addHeader("Content-Length", "" + byteArrayOutputStream.size());
        ServletOutputStream outputstream = response.getOutputStream();	//取得输出流
        byteArrayOutputStream.writeTo(outputstream);					//写到输出流
        byteArrayOutputStream.close();									//关闭
        outputstream.flush();											//刷数据
    }
}
